package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CarMapper;
import com.ruoyi.system.domain.Car;
import com.ruoyi.system.service.ICarService;

/**
 * 汽车Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-11
 */
@Service
public class CarServiceImpl implements ICarService
{
    @Autowired
    private CarMapper carMapper;

    /**
     * 查询汽车
     *
     * @param carId 汽车主键
     * @return 汽车
     */
    @Override
    public Car selectCarByCarId(Long carId)
    {
        return carMapper.selectCarByCarId(carId);
    }

    /**
     * 查询汽车列表
     *
     * @param car 汽车
     * @return 汽车
     */
    @Override
    public List<Car> selectCarList(Car car)
    {
        return carMapper.selectCarList(car);
    }

    /**
     * 新增汽车
     *
     * @param car 汽车
     * @return 结果
     */
    @Override
    public int insertCar(Car car)
    {
        return carMapper.insertCar(car);
    }

    /**
     * 修改汽车
     *
     * @param car 汽车
     * @return 结果
     */
    @Override
    public int updateCar(Car car)
    {
        return carMapper.updateCar(car);
    }

    /**
     * 批量删除汽车
     *
     * @param carIds 需要删除的汽车主键
     * @return 结果
     */
    @Override
    public int deleteCarByCarIds(Long[] carIds)
    {
        return carMapper.deleteCarByCarIds(carIds);
    }

    /**
     * 删除汽车信息
     *
     * @param carId 汽车主键
     * @return 结果
     */
    @Override
    public int deleteCarByCarId(Long carId)
    {
        return carMapper.deleteCarByCarId(carId);
    }

    @Override
    public String importCars(List<Car> carList, boolean updateSupport) {
        if (StringUtils.isNull(carList) || carList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Car car: carList)
        {
            try
            {

                this.insertCar(car);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、汽车名称 ").append(car.getCarBrand()).append(" 导入成功");

            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、汽车名称 " + car.getCarBrand() + " 导入失败：";
                failureMsg.append(msg).append(e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();

    }
}
